using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Goods.Dto;
using JTZY.Model.Goods;
using JTZY.Service.Goods.IGoodsService;
using JTZY.Admin.WebApi.Filters;
using JTZY.Service.Goods;
using Aliyun.OSS.Util;

//创建时间：2024-11-29
namespace JTZY.Admin.WebApi.Controllers.Goods
{
    /// <summary>
    /// 良品
    /// </summary>
    [Verify]
    [Route("goods/Corecmsusergoods")]
    public class CorecmsusergoodsController : BaseController
    {
        /// <summary>
        /// 良品接口
        /// </summary>
        private readonly ICorecmsusergoodsService _CorecmsusergoodsService;

        public CorecmsusergoodsController(ICorecmsusergoodsService CorecmsusergoodsService)
        {
            _CorecmsusergoodsService = CorecmsusergoodsService;
        }

        /// <summary>
        /// 查询良品列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsusergoods:list")]
        public IActionResult QueryCorecmsusergoods([FromQuery] CorecmsusergoodsQueryDto parm)
        {
            parm.UserId = HttpContext.GetUId();

            var response = _CorecmsusergoodsService.GetGoodsList(parm);
            return SUCCESS(response);
        }


        [HttpPost("marketable")]
        [ActionPermissionFilter(Permission = "corecmsagentproducts:edit")]
        [Log(Title = "代理货品池", BusinessType = BusinessType.UPDATE)]
        public IActionResult Marketable([FromBody] CorecmsusergoodsDto parm)
        {
            var modal = parm.Adapt<Corecmsusergoods>().ToUpdate(HttpContext);
            modal.Marketable = modal.Marketable == 1 ? 0 : 1;
            modal.UserId = HttpContext.GetUId();
            //上架需要刷新库存价格 暂未实现
            var response = _CorecmsusergoodsService.Marketable(modal);

            // return ToResponse(response);
            if (response.status)
            {
                return SUCCESS(modal);
            }
            else 
            {
                return ToResponse(ResultCode.CUSTOM_ERROR, response.msg);
            }

        }

        /// <summary>
        /// 查询良品详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsusergoods:query")]
        public IActionResult GetCorecmsusergoods(long Id)
        {
            var response = _CorecmsusergoodsService.GetInfo(Id);
            
            var info = response.Adapt<CorecmsusergoodsDto>();
            return SUCCESS(info);
        }


        [HttpGet("getinfo/{goodId}")]
        public IActionResult GetList([FromRoute] int goodId)
        {
            long UserId = HttpContext.GetUId();

            var response = _CorecmsusergoodsService.GetInfo(UserId, goodId);
            var info = response.Adapt<CorecmsusergoodsDto>();

            return SUCCESS(info);
        }

        /// <summary>
        /// 添加良品
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsusergoods:add")]
        [Log(Title = "良品", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsusergoods([FromBody] CorecmsusergoodsDto parm)
        {
            var modal = parm.Adapt<Corecmsusergoods>().ToCreate(HttpContext);

            var response = _CorecmsusergoodsService.AddCorecmsusergoods(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 更新良品
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsusergoods:edit")]
        [Log(Title = "良品", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsusergoods([FromBody] CorecmsusergoodsDto parm)
        {
            var modal = parm.Adapt<Corecmsusergoods>().ToUpdate(HttpContext);
            var response = _CorecmsusergoodsService.UpdateCorecmsusergoods(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除良品
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsusergoods:delete")]
        [Log(Title = "良品", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsusergoods([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CorecmsusergoodsService.Delete(idArr));
        }

        /// <summary>
        /// 导出商品表
        /// </summary>
        /// <returns></returns>
        [Log(Title = "商品表", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
        [HttpGet("export")]
        [ActionPermissionFilter(Permission = "corecmsusergoods:export")]
        public IActionResult Export([FromQuery] CorecmsusergoodsQueryDto parm)
        {
            parm.UserId = HttpContext.GetUId();
            parm.PageNum = 1;
            parm.PageSize = 100000;
            var list = _CorecmsusergoodsService.ExportList(parm).Result;
            if (list == null || list.Count <= 0)
            {
                return ToResponse(ResultCode.FAIL, "没有要导出的数据");
            }
            var result = ExportExcelMini(list, "商品表", "商品表");
            return ExportExcel(result.Item2, result.Item1);
        }
        //[HttpPost("price")]
        //[ActionPermissionFilter(Permission = "corecmsagentproducts:edit")]
        //[Log(Title = "代理货品池", BusinessType = BusinessType.UPDATE)]
        //public IActionResult ChangePrice([FromBody] ChangePriceDto parm)
        //{
        //    var modal = parm.Adapt<Corecmsusergoods>().ToUpdate(HttpContext);
        //    modal.Marketable = modal.Marketable == 1 ? 0 : 1;
        //    modal.UserId = HttpContext.GetUId();
        //    //上架需要刷新库存价格 暂未实现
        //    var response = _CorecmsusergoodsService.Marketable(modal);

        //    return ToResponse(response);
        //}
    }
}